package com.andyage.admin.service.impl;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.andyage.admin.service.IAditemService;
import com.andyage.bce.Sample;
import com.andyage.bce.UploadFile;
import com.andyage.util.JsonContent;
import com.andyage.util.LogUtil;
import com.andyage.util.MyConfig;
import com.andyage.admin.bean.AdLogBean;
import com.andyage.admin.bean.AditemBean;
import com.andyage.admin.bean.AdprojectBean;

import net.sf.json.JSONObject;

import com.andyage.admin.dao.AdItemMapper;
import com.andyage.admin.dao.AdLogMapper;
import com.andyage.admin.dao.AdProjectMapper;
@Service
public class AditemServiceImpl implements IAditemService {
	public  String CONTENT_TYPE="text/html;charset=utf-8";
	@Resource
	private AdItemMapper aditemMapper;
	@Resource
	private AdLogMapper adlogMapper;
	@Resource
	private AdProjectMapper adprojectMapper;
	@Override
	public void queryAdItemByCondition(HttpServletRequest request, HttpServletResponse response, AditemBean aditem) throws Exception {
		// TODO Auto-generated method stub
		List<AditemBean> list= aditemMapper.queryAdItemByCondition(aditem);
		int total=aditemMapper.queryAdItemCount(aditem);
		String json=JsonContent.getJsonArray(list, total);
		response.setContentType(CONTENT_TYPE);
		response.getWriter().print(json);
	}

	@Override
	public void updateAditem(HttpServletRequest request, HttpServletResponse response, AditemBean aditem)throws Exception  {
		String savePath=request.getSession().getServletContext().getRealPath("/item/");//存储路径
		String itemfilename=aditem.getItemId()+".item";//文件名称
		String newPath=savePath+itemfilename;
		File file=new File(newPath);
		if(!file.getParentFile().exists()){
			file.getParentFile().mkdirs();
		}
		if(!file.exists()){
			file.createNewFile();
		}else{
			file.delete();
		}
		String itemContent=request.getParameter("itemContent");//获得子项目的内容
		byte[] b=itemContent.getBytes("utf-8");
		OutputStream os=new FileOutputStream(file);
		os.write(b);
		os.close();
		UploadFile upload=new UploadFile();
		upload.fileUploadObject(Sample.createBosClient(), MyConfig.BUCKET_NAME, itemfilename,newPath);
		String bceURL=upload.generatePresignedUrl(Sample.createBosClient(), MyConfig.BUCKET_NAME, itemfilename, -1);
		aditem.setItemContentUrl(bceURL);
		int flag=aditemMapper.updateAditem(aditem);
		AdLogBean adlog = LogUtil.addLog((String)request.getSession().getAttribute("AccountId"), "修改回报", request.getRequestURI(),aditem.getItemTitle());
		adlogMapper.insertLog(adlog);
		JSONObject json = new JSONObject();
		if (flag!=0) {
			json.accumulate("result", 1);
			json.accumulate("tips", "修改成功");
		} else {
			json.accumulate("result", 0);
			json.accumulate("tips", "修改失败");
		}
		response.setContentType(CONTENT_TYPE);
		response.getWriter().print(json);
	}

	@Override
	public void addAditem(HttpServletRequest request, HttpServletResponse response, AditemBean aditem) throws Exception {
		String projectId = aditem.getItemProjectId();
		if(!"".equals(projectId)&&projectId!=null){
		AdprojectBean projectBean = adprojectMapper.queryProjectInfoById(projectId);
		if(projectBean!=null){
			Integer projectStatus = projectBean.getProjectStatus();
			if(projectStatus==1){
				aditem.setItemStatus(0);//0表示可支持
			}else{
				aditem.setItemStatus(1);//1表示不可支持
			}
		}
		}
		String savePath=request.getSession().getServletContext().getRealPath("/item/");//存储路径
		String itemfilename=aditem.getItemId()+".item";//文件名称
		String newPath=savePath+itemfilename;
		File file=new File(newPath);
		if(!file.getParentFile().exists()){
			file.getParentFile().mkdirs();
		}
		if(!file.exists()){
			file.createNewFile();
		}else{
			file.delete();
		}
		String itemContent=request.getParameter("itemContent");//获得子项目的内容
		byte[] b=itemContent.getBytes("utf-8");
		OutputStream os=new FileOutputStream(file);
		os.write(b);
		os.close();
		UploadFile upload=new UploadFile();
		upload.fileUploadObject(Sample.createBosClient(), MyConfig.BUCKET_NAME, itemfilename,newPath);
		String bceURL=upload.generatePresignedUrl(Sample.createBosClient(), MyConfig.BUCKET_NAME, itemfilename, -1);
		aditem.setItemContentUrl(bceURL);
		int flag=aditemMapper.addAditem(aditem);
		AdLogBean adlog = LogUtil.addLog((String)request.getSession().getAttribute("AccountId"), "新增回报", request.getRequestURI(),aditem.getItemTitle());
		adlogMapper.insertLog(adlog);
		JSONObject json = new JSONObject();
		if (flag!=0) {
			json.accumulate("result", 1);
			json.accumulate("tips", "新增成功");
		} else {
			json.accumulate("result", 0);
			json.accumulate("tips", "新增失败");
		}
		response.setContentType(CONTENT_TYPE);
		response.getWriter().print(json);
	}

	@Override
	public void deleteAditem(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// TODO Auto-generated method stub
		int flag=aditemMapper.deleteAditem(request.getParameter("itemId"));
		AdLogBean adlog = LogUtil.addLog((String)request.getSession().getAttribute("AccountId"), "删除回报", request.getRequestURI(),request.getParameter("itemTitle"));
		adlogMapper.insertLog(adlog);
		JSONObject json = new JSONObject();
		if (flag!=0) {
			json.accumulate("result", 1);
			json.accumulate("tips", "删除成功");
		} else {
			json.accumulate("result", 0);
			json.accumulate("tips", "删除失败");
		}
		response.setContentType(CONTENT_TYPE);
		response.getWriter().print(json);
	}

}
